package com.zzl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zzl.gulimall.product.entity.AttrEntity;
import com.zzl.gulimall.product.service.CategoryService;
import com.zzl.gulimall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zzl.gulimall.product.entity.AttrGroupEntity;
import com.zzl.gulimall.product.service.AttrGroupService;
import com.zzl.common.utils.PageUtils;
import com.zzl.common.utils.R;



/**
 *
 * @author zzl
 * @email zhuangzhongliang@163.com
 * @date 2021-03-10 14:06:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    ///1/noattr/relation
    ///attr/relation

    @PostMapping("/attr/relation/delete")
    public R removeAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVoList){

        attrGroupService.removeAttrRelation(attrRelationVoList);
        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVoList){

        attrGroupService.saveAttrRelation(attrRelationVoList);
        return R.ok();
    }

    @GetMapping("{attrgroupId}/{type}/relation")
    public R getAttrRelationList(@RequestParam Map<String, Object> params,
                                 @PathVariable(value = "attrgroupId")Long attrgroupId,
                                 @PathVariable(value = "type")String type){
        PageUtils pageUtils =  attrGroupService.getAttrRelationList(params,attrgroupId,type);
        if ("attr".equalsIgnoreCase(type)){
            return R.ok().put("data",pageUtils.getList());
        }else {
            return R.ok().put("page",pageUtils);
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(value = "catelogId") Integer catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPageByCatelogId(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
